package com.oazon.sonos.core;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moco.launcher.bean.MOCO_CONSTANTS;
import com.moco.launcher.http.HttpUtil;
import com.moco.launcher.listener.CloudListener;
import com.oazon.common.Logger;
import com.oazon.common.util.RequestManager;
import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.lan.util.UrlDecode;
import com.oazon.moco.playlist.bean.AbstractMedia;
import com.oazon.moco.playlist.bean.MediaList;
import com.oazon.sonos.bean.GetMetadata;
import com.oazon.sonos.bean.GetMetadataResponse;
import com.oazon.sonos.bean.Search;
import com.oazon.sonos.bean.SearchResponse;
import com.oazon.sonos.listener.SonosListener;

public class O2Controler implements ISonosSoap {

	private static O2Controler CONTROL = null;
	private String TAG = "ISonosSoap";

	private O2Controler() {

	}

	public static O2Controler getInstance(Context context) {
		if (CONTROL == null) {
			CONTROL = new O2Controler();
		}
		return CONTROL;
	}

	@Override
	public void getMediaURI(Context context, String id, String provider,
			final SonosListener listener) {

		String url = MOCO_CONSTANTS.GET_MEDIA_URI;
		Map<String, String> params = new HashMap<String, String>();

		params.put("id", id);
		params.put("provider", provider);

		url = RequestManager.spliceUri(params, url, null);
		RequestManager.sendRequest(context, url, new CloudListener() {

			@Override
			public void onResponse(String response) {
				try {
					JSONObject jsonObject = JSON.parseObject(response);
					listener.onResponse(Uri.parse(jsonObject
							.getString("getMediaURIResult")));
				} catch (Exception e) {
					if (response == null) {
						onError(0, "nullpoint setVideoPath");
					}
				}
			}

			@Override
			public void onError(int err, String msg) {
				listener.onError(err, msg);
			}
		});

	}

	@Override
	public void getMetadata(Context context, GetMetadata parameters,
			final SonosListener listener) {
		if (parameters == null) {
			return;
		}
		if (parameters.getId() == null) {
			parameters.setId(MOCO_CONSTANTS.ROOT_ID);
		}

		if (parameters.getIndex() < 0) {
			parameters.setIndex(0);
		}
		if (parameters.getCount() <= 0) {
			parameters.setCount(1);
		}
		if (parameters.getProvider() == null) {
			parameters.setProvider(MOCO_CONSTANTS.RECOMMEND_ROOT);
		}
		String url = MOCO_CONSTANTS.MAIN //
				+ "getMetadata?id=" + parameters.getId() //
				+ "&index=" + parameters.getIndex()//
				+ "&count=" + parameters.getCount() //
				+ "&recursive=" + parameters.isRecursive()//
				+ "&provider=" + parameters.getProvider()
				+ "&mode=test";
		RequestManager.sendRequest(context, url, new CloudListener() {

			@Override
			public void onResponse(String response) {
				GetMetadataResponse metadataResult = RequestManager
						.parseJsonResponse(response);
				listener.onResponse(metadataResult);
			}

			@Override
			public void onError(int err, String msg) {
				listener.onError(err, msg);
			}
		});
	}

	@Override
	public void search(Context context, Search parameters,
			final SonosListener listener) {
		Logger.d(TAG, "o2->search:" + parameters);
		if (parameters == null) {
			return;
		}
		String url = null;
		try {
			url = MOCO_CONSTANTS.MAIN //
					+ "search?id=" + parameters.getId() //
					+ "&index=" + parameters.getIndex()//
					+ "&term=" + URLEncoder.encode(parameters.getTerm(), "utf-8")//
					+ "&count=" + parameters.getCount() //
					+ "&provider=" + parameters.getProvider();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		RequestManager.sendRequest(context, url, new CloudListener() {

			@Override
			public void onResponse(String response) {
				Log.d(TAG, response);
				GetMetadataResponse<AbstractMedia> metadataResult = RequestManager
						.parseJsonResponse(response);
				SearchResponse resultObj = new SearchResponse();
				if (metadataResult != null) {
					List<AbstractMedia> items = metadataResult
							.getGetMetadataResult()
							.getMediaCollectionOrMediaMetadata();
					MediaList ml = new MediaList();
					ml.setMediaCollectionOrMediaMetadata(items);
					resultObj.setSearchResult(ml);
				} else {
					resultObj.setSearchResult(null);
				}
				listener.onResponse(resultObj);
			}

			@Override
			public void onError(int err, String msg) {
				listener.onError(err, msg);
			}
		});

	}

	@Override
	public void registMcServiceMsg(McServiceMsg msg) {

	}

	@Override
	public AbsServiceHelper getServiceHelper() {
		return null;
	}
}
